//package org.example.shiro;
//
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.*;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import org.apache.shiro.subject.Subject;
//import org.example.common.exception.user.CaptchaException;
//import org.example.common.exception.user.UserNotExistsException;
//import org.example.model.User;
//import org.example.service.UserService;
//import org.springframework.beans.factory.annotation.Autowired;
//
////自定义Realm处理登录权限
//public class UserRealm2 extends AuthorizingRealm {
//
//    @Autowired
//    private UserService userService;
//    //执行认证逻辑
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
//        System.out.println("执行认证逻辑");
//        //1、判断用户名 token中的用户信息是登录时候传进来的
//        UsernamePasswordToken token = (UsernamePasswordToken) arg0;
//        String username = token.getUsername();
//        String password = "";
//        User user = userService.findByName(token.getUsername());
////        System.out.println(user);
//        if(token.getPassword() != null){
//            password = new String(token.getPassword());
//        }
////        User user = null;
//        try {
//            user = userService.login(user);
//            //user = userService.login2(username,password);
//        }catch (CaptchaException e){
//            throw new AuthenticationException(e.getMessage(),e);
//        }catch (UserNotExistsException e){
//            throw new UnknownAccountException(e.getMessage(),e);
//        }catch (Exception e){
//            System.out.println("对用户【"+ username+"】进行登录验证..验证未通过{}");
//            throw new AuthenticationException(e.getMessage(),e);
//        }
////        if(user == null){
////            //用户名不存在
////            return null;
////        }
//        //2、判断密码
//        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user,password,getName());
//        System.out.println(info);
//        return info;
//
//    }
//
//    //执行授权逻辑
//    @Override
//
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0){
//        //doGetAuthorizationInfo方法可能会执行多次，权限判断次数多少，就会执行多少次
//        System.out.println("执行授权逻辑1");
//        System.out.println("执行授权逻辑2");
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        //添加资源的授权字符串
//        //info.addStringPeimission("user:add);
//        //到数据库查询当前登录用户的授权字符串
//        //获取当前登录用户
//        Subject subject = SecurityUtils.getSubject();
//        User user = (User)subject.getPrincipal();
//        User dbUser = userService.findById(user.getId());
//        info.addStringPermission("user:add");
////        info.addStringPermission();
//        return info;
//    }
//}
